﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web;

namespace System.Web.Mvc
{
    public static class UrlHelperExt
    {
        /// <summary>
        /// 安全的获取控制器-视图的url
        /// </summary>
        /// <param name="urlHelper"></param>
        /// <param name="actionName">action名</param>
        /// <returns></returns>
        public static string Position(this UrlHelper urlHelper, string actionName)
        {
            return urlHelper.Action(actionName);
        }

        /// <summary>
        /// 安全的获取控制器-视图的url
        /// </summary>
        /// <typeparam name="T">控制器类</typeparam>
        /// <param name="urlHelper"></param>
        /// <param name="actionName">action名</param>
        /// <param name="areaName">区域名</param>
        /// <returns></returns>
        public static string Position<T>(this UrlHelper urlHelper, string actionName = null, string areaName = null)
        {
            string name = typeof(T).Name;
            string controllerName = name.EndsWith("Controller")
                ? name.Substring(0, name.Length - 10) : name;
            return urlHelper.Action(actionName, controllerName, new { area = areaName});
        }

    }
}
